package cn.wolfcode.redis;

import org.junit.jupiter.api.Test;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class SpringBootRedis {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //操作String类型：添加name：xiaolin，age：10的数据
    @Test
    public void testRedisTemplate() {
        redisTemplate.opsForValue().set("name", "xiaolin");
        redisTemplate.opsForValue().set("age", "10");
        //增加年龄2岁
        redisTemplate.opsForValue().increment("age", 2L);
        //减少年龄2岁
        redisTemplate.opsForValue().decrement("age", 2L);
        //通过key获取value值
        System.out.println(redisTemplate.opsForValue().get("name"));
        //设置失效时间
        redisTemplate.opsForValue().set("name","xiaodong",10, TimeUnit.HOURS);

    }

    @Test
    public void testRedisTemplate2() {
        //操作hash类型：存入一个hash对象
        redisTemplate.opsForHash().put("Employee", "name", "xiaobai");
        redisTemplate.opsForHash().put("Employee", "age", "18");
        //根据hash对象键取值
        System.out.println(redisTemplate.opsForHash().get("Employee", "name"));
        //判断hash对象中是否存在age
        System.out.println(redisTemplate.opsForHash().hasKey("Employee", "age"));
        //根据hash对象key删除hash对象键值对
        redisTemplate.opsForHash().delete("Employee","age");
    }

    @Test
    public void testRedisTemplate3(){
        //操作list类型, 往列表右边添加数据
        redisTemplate.opsForList().rightPush("hobby","java");
       //弹出（删除）最左边数据
        redisTemplate.opsForList().leftPop("hobby");
        //弹出（删除）最右边数据
        redisTemplate.opsForList().rightPop("hobby");
        //获取指定key的长度
        System.out.println(redisTemplate.opsForList().size("hobby"));
        //范围显示列表数据,全显示则设置0 -1
        System.out.println(redisTemplate.opsForList().range("hobby", 0L, -1L));

    }

    @Test
    public void testRedisTemplate4(){
       //操作set类型
        redisTemplate.opsForSet().add("city","gz");
        //获取set集合列表里面元素
        System.out.println(redisTemplate.opsForSet().members("city"));
        //删除set集合里面指定的key值
        redisTemplate.opsForSet().remove("city","sz");
        //随机弹出集合中的元素
        redisTemplate.opsForSet().pop("city");
        //返回key1中特有元素(差集)
        System.out.println(redisTemplate.opsForSet().difference("city", "city2"));
        //返回两个set集合的交集
        System.out.println(redisTemplate.opsForSet().intersect("city", "city2"));
        //返回两个set集合的并集
        System.out.println(redisTemplate.opsForSet().union("city", "city2"));
        //返回set集合中元素个数
        System.out.println(redisTemplate.opsForSet().size("city"));
    }

    @Test
    public void testRedisTemplate5(){
        //操作zset,存入分数和名称
        redisTemplate.opsForZSet().add("dept","小东",60);
        redisTemplate.opsForZSet().add("dept","小南",30);
        //偏移名称对应的分数(加分数)
        redisTemplate.opsForZSet().incrementScore("dept","小东",10);
        //按照分数升序输出名称(索引0，-1显示全部)
        System.out.println(redisTemplate.opsForZSet().range("dept", 0, -1));
        //按照分数降序输出名称
        redisTemplate.opsForZSet().reverseRange("dept",0,-1);
        //升序返回排名（返回索引）
        System.out.println(redisTemplate.opsForZSet().rank("dept", "小东"));
        //降序返回排名
        System.out.println(redisTemplate.opsForZSet().reverseRank("dept", "小南"));
        //返回元素个数
        System.out.println(redisTemplate.opsForZSet().zCard("class1"));
    }

}
